package

        com.rfid.system.service.impl;

import java.util.Date;
import java.util.List;

import com.rfid.common.exception.BusinessException;
import com.rfid.common.utils.DateUtils;
import com.rfid.common.utils.StringUtils;
import com.rfid.system.domain.TCustomerInfo;
import com.rfid.system.service.common.CodeRfidGenerateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rfid.system.mapper.TProductInfoMapper;
import com.rfid.system.domain.TProductInfo;
import com.rfid.system.service.ITProductInfoService;
import com.rfid.common.core.text.Convert;

/**     
 * 产品信息Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-06
 */
@Service
public class TProductInfoServiceImpl implements ITProductInfoService {

    private static final Logger log = LoggerFactory.getLogger(TProductInfoServiceImpl.class);

    @Autowired
    private TProductInfoMapper tProductInfoMapper;

    @Autowired
    private CodeRfidGenerateService codeGenerateService;

    /**
     * 查询产品信息
     *
     * @param id 产品信息ID
     * @return 产品信息
     */
    @Override
    public TProductInfo selectTProductInfoById(Long id) {
        return tProductInfoMapper.selectTProductInfoById(id);
    }

    /**
     * 查询产品信息列表
     *
     * @param tProductInfo 产品信息
     * @return 产品信息
     */
    @Override
    public List<TProductInfo> selectTProductInfoList(TProductInfo tProductInfo) {
        return tProductInfoMapper.selectTProductInfoList(tProductInfo);
    }

    /**
     * 新增产品信息
     *
     * @param tProductInfo 产品信息
     * @return 结果
     */
    @Override
    public int insertTProductInfo(TProductInfo tProductInfo) {
        tProductInfo.setCreateTime(DateUtils.getNowDate());
        return tProductInfoMapper.insertTProductInfo(tProductInfo);
    }

    /**
     * 修改产品信息
     *
     * @param tProductInfo 产品信息
     * @return 结果
     */
    @Override
    public int updateTProductInfo(TProductInfo tProductInfo) {
        tProductInfo.setUpdateTime(DateUtils.getNowDate());
        return tProductInfoMapper.updateTProductInfo(tProductInfo);
    }

    /**
     * 删除产品信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTProductInfoByIds(String ids) {
        return tProductInfoMapper.deleteTProductInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除产品信息信息
     *
     * @param id 产品信息ID
     * @return 结果
     */
    @Override
    public int deleteTProductInfoById(Long id) {
        return tProductInfoMapper.deleteTProductInfoById(id);
    }

    @Override
    public TProductInfo getLastData() {
        return tProductInfoMapper.getLastData();
    }

    @Override
    public String importUser(List<TProductInfo> cusList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(cusList) || cusList.size() == 0) {
            throw new BusinessException("导入产品数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TProductInfo cus : cusList){
            try {
                if(StringUtils.isEmpty(cus.getProductName())){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、产品名称不能为空");
                }
                TProductInfo lastData=this.getLastData();
                String productNo=codeGenerateService.generateCode("CUSTOMER_CODE_RULE",lastData.getProductNo(),lastData);
                cus.setLastSerialNum("0");
                cus.setProductNo(productNo);
                cus.setCreateTime(new Date());
                cus.setUpdateTime(new Date());
                cus.setCreateBy(operName);
                this.insertTProductInfo(cus);
                successNum++;
                successMsg.append("<br/>" + successNum + "、产品名称:" + cus.getProductName() + " 导入成功");
            }catch (Exception e){
                failureNum++;
                String msg = "<br/>" + failureNum + "、产品名称:"+ cus.getProductName() + " 导入失败";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
